/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lookMessage;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpSession;
import loginRegister.LoginBean;

/**
 *
 * @author DELL
 */
public class LookMessageServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet LookMessageServlet</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet LookMessageServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String userName = null;
               userName =request.getParameter("userName");
        log(userName);
        
                      
                HttpSession session = request.getSession();
                ArrayList login = (ArrayList) session.getAttribute("login");
                if (login == null || login.size() == 0) {
                    response.sendRedirect("http://localhost:8081/PersonInforMSys/login.jsp");
                } else {
                    for (int i = login.size() - 1; i >= 0; i--) {

                        LoginBean nn = (LoginBean) login.get(i);
                        userName = nn.getUserName();

                    }
                }
                
                log(userName);
        
        try {
            Connection con = null;
            java.sql.Statement stmt = null;
            ResultSet rs = null;
            Class.forName("com.mysql.jdbc.Driver");

            String url = "jdbc:mysql://localhost:3306/person";

            con = DriverManager.getConnection(url, "skyline", "739349");

            stmt = con.createStatement();
            String sql = "select * from user where userName='" + userName + "'";

            rs = stmt.executeQuery(sql);
            LookMessageBean mm = new LookMessageBean();
            while (rs.next()) {
                mm.setName(rs.getString("name"));
                mm.setSex(rs.getString("sex"));
                mm.setBirth(rs.getString("birth"));
                mm.setNation(rs.getString("nation"));

                mm.setEdu(rs.getString("edu"));
                mm.setPhone(rs.getString("phone"));
                mm.setAddr(rs.getString("addr"));
                mm.setEmail(rs.getString("email"));

            }
           
            ArrayList al = new ArrayList();
            al.add(mm);
            session.setAttribute("wordlist", al);
            rs.close();
            stmt.close();
            con.close();
            response.sendRedirect("http://localhost:8081/PersonInforMSys/lookMessage/lookMessage.jsp");

        } catch (ClassNotFoundException ex) {
            Logger.getLogger(LookMessageServlet.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(LookMessageServlet.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);

    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
